<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import i18n from '@/lang'
require('echarts/theme/macarons') // echarts theme
// import resize from './mixins/resize'

export default {
  // mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    title: {
      type: String,
      default: '甘特图'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      height: '300px'
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'default')
      this.setOptions()
    },
    setOptions({ valueY, start, end, label } = {}) {
      if (valueY !== undefined) {
        this.height = valueY.length * 100 + 'px'
      }
      this.chart.setOption({
        title: {
          text: this.title
        },
        animation: false,
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'line' // 默认为直线，可选为：'line' | 'shadow'
          },
          formatter: function(params) {
            return params[0].name + '<br/>' +
            params[1].seriesName + params[1].value + '<br/>' +
            params[0].seriesName + params[0].value
          }
        },
        legend: {
          data: ['计划时间']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        yAxis: {
          type: 'category',
          splitLine: { show: false },
          axisTick: { show: false },
          axisLine: { show: false },
          axisLabel: {
            show: true,
            textStyle: {
              color: 'blue',
              fontSize: 14
            }
          },
          data: valueY
        },
        xAxis: {
          type: 'time',
          position: 'top',
          splitLine: {
            show: false,
            lineStyle: {
              color: ['#E9EDFF']
            }
          },
          axisLine: {
            show: true
          },
          axisTick: {
            show: false,
            lineStyle: {
              color: '#929ABA'
            }
          },
          axisLabel: {
            color: '#929ABA',
            inside: false,
            align: 'center',
            textStyle: {
              color: 'Black',
              fontSize: 14
            }
          }
        },
        series: [
          {
            name: '原始',
            type: 'bar',
            stack: '时间',
            itemStyle: {
              show: false,
              color: 'rgba(0,0,0,0)'
            }
          },
          {
            name: i18n.t('common.planEnd'),
            barWidth: 20,
            type: 'bar',
            stack: '时间',
            label: {
              normal: {
                show: true,
                position: 'top'
              }
            },
            itemStyle: {
              color: function(params) {
                // var idx = params.dataIndex
                return 'green'
              }// ,
              // barBorderColor: 'white'
            },
            data: end
          },
          {
            name: i18n.t('common.planStart'),
            barWidth: 20,
            type: 'bar',
            stack: '时间',
            label: {
              normal: {
                show: true,
                position: 'top'
              }
            },
            itemStyle: {
              color: 'white',
              barBorderColor: 'white'
            },
            data: start
          }
        ]
      })
    }
  }
}
</script>
